
{-# LANGUAGE TypeFamilies, FlexibleContexts #-}
{-# LANGUAGE DeriveDataTypeable #-}


module ORel.Relation where

import Data.Typeable
import Data.List
import qualified Data.Map as Map

import ORel.ItemId
import ORel.SetProp
import ORel.Types
import ORel.Object


type family PlayerType relNm roleNm -- =  playerType


data Relation rel
    = Relation ItemId (Map.Map String [ItemId])
    deriving (Show, Typeable)


infixl 6 .+.
(.+.) ::
    (Show roleNm
    ,Subtype playerTyp (PlayerType relNm roleNm)
    )
    => Relation relNm -> Set roleNm (Object playerTyp) -> Relation relNm
(Relation id rel) .+. (role := Object objId _)
    = Relation id $ Map.update (Just . (objId:)) (show role) rel


infixl 6 .-.
(.-.) ::
    (Show roleNm
    ,Subtype playerTyp (PlayerType relNm roleNm)
    )
    => Relation relNm -> Set roleNm (Object playerTyp) -> Relation relNm
(Relation id rel) .-. (role := Object objId _)
    = Relation id $ Map.update (Just . (delete objId)) (show role) rel

